package com.example.androidcompletesec.cp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class WordDBManager extends SQLiteOpenHelper {
	static final String DATABASE_NAME = "EngWordSec.db";
	static final String TABLE_DIC = "dic";
	static final int DATABASE_VER = 1;
	
	Context mContext;
	private static WordDBManager mWordDBManager;
	
	public WordDBManager(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		mContext = context;
	}
	
	public static WordDBManager getInstance(Context context){
		if(mWordDBManager == null){
			mWordDBManager = new WordDBManager(context, DATABASE_NAME, null, DATABASE_VER); 
		}
		return mWordDBManager;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS dic ("
				+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "eng TEXT, "
				+ "han TEXT);");
		db.execSQL("insert into dic values(null, 'boy', '소년');");
		db.execSQL("insert into dic values(null, 'girl', '소녀');");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("Drop table if exists dic");
		onCreate(db);
	}
	
	public long insert(ContentValues addRowValue){
		return getWritableDatabase().insert(TABLE_DIC, null, addRowValue);
	}
	
	public int update(ContentValues updateRowValue, String whereClasue, String[] whereArgs){
		return getWritableDatabase().update(TABLE_DIC, updateRowValue, whereClasue, whereArgs);
	}
	
	public int delete(String whereClause, String[] whereArgs){
		return getWritableDatabase().delete(TABLE_DIC, whereClause, whereArgs);
	}
	
	public Cursor select(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
		return getReadableDatabase().query(TABLE_DIC, columns, selection, selectionArgs, groupBy, having, orderBy);
	}
}

